package DAO.impl;

import DAO.BaseDao;
import Service.impl.Test;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.InputMismatchException;
import java.util.Scanner;


public class StoreService extends BaseDao {
    /*
     * 宠物商店培育新宠物
     */
    public void PetStoreMake(String storeName) throws SQLException {
        Test test = new Test();
        System.out.println("请 输入数字进行选择：\n1.查询店内宠物\n2.培育新宠物\n3.退出登录");
        try {
            Scanner input=new Scanner(System.in);
            int choise7=input.nextInt();
            if(choise7<1||choise7>3){
                System.out.println("输入有误");
                PetStoreMake(storeName);
            }else{
                switch (choise7) {
                    case 1:
                        storePetQuery(storeName);
                        break;
                    case 2:
                        storeAddPet(storeName);
                        break;
                    case 3:
                        //退出登录，返回上一级
                        test.login();
                        break;

                    default:
                        break;
                }
            }
        } catch (InputMismatchException e) {
            System.out.println("输入有误");
            PetStoreMake(storeName);
        }
    }

    /**
     * 宠物商店培育新宠物的方法
     * @param storeName
     * @throws SQLException
     */
    public void storeAddPet(String storeName) throws SQLException {
        System.out.println("请输入你想添加的宠物的类型：");
        Scanner input=new Scanner(System.in);
        String typename=input.next();
        System.out.println("请输入该宠物的名字：");
        String petname=input.next();
        //查询该商店的ID
        String sql14="SELECT ID from petstore where name='"+storeName+"'";
        rs=search(sql14);
        int id=0;
        if(rs.next()){
            id=rs.getInt("ID");
        }
        try {
            conn=getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sql13="insert into pet (name,typename,health,love,birthday,store_id,neworold)VALUES(?,?,1,100,?,"+id+",'new')";
        //获取当前时间，作为宠物的生日
        Long time1=System.currentTimeMillis();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String birth=sdf.format(time1);
        int a=update(sql13, petname,typename,birth);
        if(a>0){
            System.out.println("培育新宠物成功");
            PetStoreMake(storeName);
        }else{
            System.out.println("培育新宠物失败");
            PetStoreMake(storeName);
        }
    }
    /**
     * 在商店登录之后进行对店内的宠物进行查询
     * @param storeName
     */
    public void storePetQuery(String storeName) {
        System.out.println("正在查询店内宠物。。。");
        try {
            conn=getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sql11="SELECT pet.ID,pet.name,typename,birthday from pet,petstore where petstore.name=? and pet.store_id=petstore.ID";
        try {
            state=conn.prepareStatement(sql11);
            rs=search(sql11, storeName);
            int i=1;
            while(rs.next()){
                int id=rs.getInt("pet.ID");
                String name=rs.getString("pet.name");
                String typename=rs.getString("typename");
                String birthday=rs.getString("birthday");
                System.out.println("第"+i+"只宠物名字："+name+",宠物类型："+typename+",生日："+birthday);
                i++;
            }
            System.out.println("----------------------------");
            PetStoreMake(storeName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
